/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.frame;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.frame
 * @date 24/04/2014 - 22:37:56
 */
public class InternalFrame extends JInternalFrame {
	
	private static final long serialVersionUID = 1562114138292237240L;
	
	private JDesktopPane owner;
	
	private boolean showExitQuestion = true;
	
	/**
	 * @param owner
	 */
	public InternalFrame(final JDesktopPane owner) {
	
		super();
		
		this.owner = owner;
		
		initGUI();
	}
	
	/**
	 * @param title
	 */
	public InternalFrame(final String title) {
	
		this(title, null);
	}
	
	/**
	 * @param title
	 * @param resizable
	 */
	public InternalFrame(final String title, final boolean resizable) {
	
		this(title, resizable, null);
	}
	
	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 */
	public InternalFrame(final String title, final boolean resizable, final boolean closable) {
	
		this(title, resizable, closable, null);
	}
	
	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 * @param maximizable
	 */
	public InternalFrame(final String title, final boolean resizable, final boolean closable, final boolean maximizable) {
	
		this(title, resizable, closable, maximizable, null);
	}
	
	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 * @param maximizable
	 * @param iconifiable
	 */
	public InternalFrame(final String title, final boolean resizable, final boolean closable, final boolean maximizable, final boolean iconifiable) {
	
		this(title, resizable, closable, maximizable, iconifiable, null);
	}
	
	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 * @param maximizable
	 * @param iconifiable
	 */
	public InternalFrame(final String title, final boolean resizable, final boolean closable, final boolean maximizable, final boolean iconifiable, final JDesktopPane owner) {
	
		super(title, resizable, closable, maximizable, iconifiable);
		
		this.owner = owner;
		
		initGUI();
	}
	
	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 * @param maximizable
	 */
	public InternalFrame(final String title, final boolean resizable, final boolean closable, final boolean maximizable, final JDesktopPane owner) {
	
		super(title, resizable, closable, maximizable);
		
		this.owner = owner;
		
		initGUI();
	}
	
	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 */
	public InternalFrame(final String title, final boolean resizable, final boolean closable, final JDesktopPane owner) {
	
		super(title, resizable, closable);
		
		this.owner = owner;
		
		initGUI();
	}
	
	/**
	 * @param title
	 * @param resizable
	 */
	public InternalFrame(final String title, final boolean resizable, final JDesktopPane owner) {
	
		super(title, resizable);
		
		this.owner = owner;
		
		initGUI();
	}
	
	/**
	 * @param title
	 */
	public InternalFrame(final String title, final JDesktopPane owner) {
	
		super(title);
		
		this.owner = owner;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		//
		final KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
		
		getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, "esc");
		getRootPane().getActionMap().put("esc", new AbstractAction() {
			
			private static final long serialVersionUID = 6353301310920897015L;
			
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
			
				exitAction();
			}
		});
		
		//
		addInternalFrameListener(new InternalFrameAdapter() {
			
			@Override
			public void internalFrameClosing(final InternalFrameEvent internalFrameEvent) {
			
				exitAction();
			}
			
			@Override
			public void internalFrameOpened(final InternalFrameEvent internalFrameEvent) {
			
				centralize();
			}
		});
		
		//
		setSize(350, 350);
		
		//
		if(getOwner() != null) {
			
			getOwner().add(this);
		}
	}
	
	/**
	 * 
	 */
	public final void centralize() {
	
		//
		if(getOwner() != null) {
			
			setBounds((getOwner().getWidth() - getWidth()) / 2, (getOwner().getHeight() - getHeight()) / 2, getWidth(), getHeight());
		}
	}
	
	/**
	 * 
	 */
	public void exitAction() {
	
		//
		if(getDefaultCloseOperation() == DO_NOTHING_ON_CLOSE && showExitQuestion()) {
			
			//
			if(JOptionPane.showConfirmDialog(null, I18N.component().reallyCloseTheScreen(getTitle() != null ? getTitle().trim() : "")) == JOptionPane.YES_OPTION) {
				
				dispose();
			}
			
		}else {
			
			switch(getDefaultCloseOperation()) {
			
				case DISPOSE_ON_CLOSE:
					
					try {
						//
						fireVetoableChange(IS_CLOSED_PROPERTY, Boolean.FALSE, Boolean.TRUE);
						
						isClosed = true;
						
						setVisible(false);
						
						firePropertyChange(IS_CLOSED_PROPERTY, Boolean.FALSE, Boolean.TRUE);
						
						dispose();
						
					}catch(PropertyVetoException pve) {
						// ignore
					}
					
					break;
				
				default:
					
					setVisible(false);
					
					//
					if(isSelected()) {
						
						try {
							//
							setSelected(false);
							
						}catch(PropertyVetoException pve) {
							// ignore
						}
					}
					
					break;
			}
		}
	}
	
	/**
	 * @return the owner
	 */
	public final JDesktopPane getOwner() {
	
		return owner;
	}
	
	/**
	 * @return the showExitQuestion
	 */
	public final boolean showExitQuestion() {
	
		return showExitQuestion;
	}
	
	/**
	 * @param showExitQuestion
	 *        the showExitQuestion to set
	 */
	public final void showExitQuestion(final boolean showExitQuestion) {
	
		this.showExitQuestion = showExitQuestion;
	}
	
	/**
	 * @param owner
	 *        the owner to set
	 */
	public void setOwner(final JDesktopPane owner) {
	
		this.owner = owner;
	}
}
